﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Clothes.BLL;
using Clothes.IBLL;
using Clothes.Model;
using Clothes.Model.District;
using Newtonsoft.Json;

namespace YongHu_UI
{
    public partial class contactDress : System.Web.UI.Page
    {
        public int idd;
        public int id;
        public string xing;
        public string shou;
        public string tu;
        public string jian;
        public int shu;
        public int? jin;
        public string di;
        CLDressIBLL dressIBLL = new CLDressBLL();
        CLUserIBLL userIBLL = new CLUserBLL();

        private District districtList;
        public contactDress()
        {
            //将district.json转换为代码
            using (FileStream stream = new FileStream(Server.MapPath("~/App_Data/district.json"), FileMode.Open))
            {
                StreamReader streamReader = new StreamReader(stream, Encoding.Default);
                string json = streamReader.ReadToEnd();
                districtList = JsonConvert.DeserializeObject<District>(json);
            }
        }

        //public void aa()
        //{
        //    ddlProvince.DataSource = districtList.Provinces;
        //    ddlProvince.DataTextField = "ProvinceName";
        //    ddlProvince.DataValueField = "ProvinceName";
        //    ddlProvince.DataBind();
        //}

        //public void bb(string bbb)
        //{
        //    ddlCity.DataSource = districtList.Provinces.SingleOrDefault(a => a.ProvinceName == bbb).Citys;
        //    ddlCity.DataTextField = "CityName";
        //    ddlCity.DataValueField = "CityName";
        //    ddlCity.DataBind();
        //}

        protected void Page_Load(object sender, EventArgs e)
        {
                //aa();
                var list = Model.IdentityManager.ReadUser();
                if (list == null)
                {
                    Response.Redirect("login.aspx");
                }
                id = Convert.ToInt32(Request.QueryString["id"]);
                idd = Model.IdentityManager.ReadUser().Id;
                CLUser clu = userIBLL.Select(idd);
                CLDress dress = dressIBLL.SelectId(id);
                xing = "";
                shou = "";
                di = "";

                if (clu != null)
                {
                    xing = clu.NickName;
                    di = clu.Address;
                    shou = clu.MobileNumber;
                }


                tu = dress.Avatar;
                jian = dress.Summary;
                shu = Convert.ToInt32(Request.QueryString["number"]);
                jin = dress.Money;


        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (xing == "" && di == "")
            {
                CLUser user1 = new CLUser()
                {
                    NickName = TextBox1.Text,
                    Avatar = null,
                    CLLoginId = idd,
                    Sex = 0,
                    Birthday = null,
                    Address = TextBox3.Text,
                    CreationTime = DateTime.Now,
                    MobileNumber = TextBox2.Text
                };
                userIBLL.InsertBLL(user1);
                Response.Redirect("contact.aspx?id=" + idd);
            }
            else
            {
                userIBLL.SelectEdit(idd, TextBox1.Text, TextBox3.Text, TextBox2.Text);
                Response.Redirect("contact.aspx?id=" + idd);
            }

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            CLDressIBLL cLDress = new CLDressBLL();
            cLDress.Edit(id, shu);
            ClientScript.RegisterStartupScript(GetType(), "loginerror", "alert('购买成功');location.href='checkout.aspx';", true);
            
        }

        //protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    bb()
        //}
    }
}